#include "widget.h"
#include "ui_widget.h"

#include <QtNetwork>
#include "nam.h"
#include "nbr.h"
//#include "t_networkaccesshttpbackend.h"
#include "t_my_httpbackend.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    //T_HttpSocketEngine *v_engine = new T_HttpSocketEngine();
	////T_HttpSocketEngineHandler *v_handler = new T_HttpSocketEngineHandler();

	////m_server.start();

}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
	m_proxy.setType(QNetworkProxy::HttpProxy);
	//m_proxy.setType(QNetworkProxy::HttpCachingProxy);
	m_proxy.setHostName("localhost");
	m_proxy.setPort(8080);
	if (m_proxy.type() != QNetworkProxy::HttpProxy)
	{
		qDebug() << "ng!";
	}
	else
	{
		qDebug() << "ok!";
	}
#if 0x0
	NetworkAccessManager nam;
	//nam.setProxy(m_proxy);
	QByteArray v_bytes = nam.doGet("http://www.google.co.jp");
	//QByteArray v_bytes = nam.doGet("https://www.google.co.jp");
	qDebug() << v_bytes;
#else
	NetworkAccessManager * nam2 = new NetworkAccessManager;
	nam2->setProxy(m_proxy);
	//T_NetworkAccessHttpBackend *v_be = new T_NetworkAccessHttpBackend;
	T_MyHttpBackend *v_be = new T_MyHttpBackend;
	QUrl v_url("http://www.google.co.jp");
	//QUrl v_url("https://www3.idpass-net.nenkin.go.jp/neko/action/z0401");
	//QUrl v_url("https://www3.idpass-net.nenkin.go.jp/neko/action/verify");

	QNetworkRequest v_request(v_url);
	QNetworkReply *v_reply = 0;
	if(false)
	{
		v_reply = nam2->createRequest(QNetworkAccessManager::GetOperation, v_request, 0);
	}
	else
	{
		qDebug() << "before createRequest2()";
		//v_reply = nam2->createRequest2(QNetworkAccessManager::GetOperation, v_request, 0, (void *)0);
		v_reply = nam2->createRequest(QNetworkAccessManager::GetOperation, v_request, 0, v_be);
		//qDebug() << "before createRequest3()";
		//v_reply = nam2->createRequest3(v_request, 0);
	}
	//QNetworkReply *v_reply = nam2->createRequest2(QNetworkAccessManager::GetOperation, v_request, 0, v_be);
	//QNetworkReply *v_reply = nam2.createRequest2(QNetworkAccessManager::GetOperation, v_request, 0, 0);
	QByteArray v_response;
	NetworkBatchRequest v_nbr(v_reply, &v_response, false);
	v_nbr.wait();
	qDebug() << v_response;
#endif
}

